package com.wenwei.sharebox.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.wenwei.sharebox.R;
import com.wenwei.sharebox.base.AtyContainer;
import com.wenwei.sharebox.base.BaseActivity;
import com.wenwei.sharebox.bean.AppMsgBean;
import com.wenwei.sharebox.net.ApiManager;
import com.wenwei.sharebox.net.BaseSubscriber;
import com.wenwei.sharebox.utils.RogerSPUtils;
import com.wenwei.sharebox.utils.UserStringUtils;
import com.wenwei.sharebox.utils.VersionUtils;
import com.wenwei.sharebox.view.CustomDelDialog;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/07/09 0009.
 */
public class SysSettingActivity extends BaseActivity {
    @Bind(R.id.base_title)
    TextView baseTitle;
    @Bind(R.id.ren_zheng_tv)
    TextView renZhengTv;

    @Override
    public int setLayoutResourceId() {
        return R.layout.activity_setting;
    }

    @Override
    protected void initView() {
        super.initView();
        baseTitle.setText("设置");
        if (UserStringUtils.getUserStats(mContext) == 1) {
            renZhengTv.setText("已认证");
        }else{
            renZhengTv.setText("未认证");
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    private CustomDelDialog mExitLoginDialog;
    private CustomDelDialog mVersionDialog;

    @OnClick({R.id.exit_login_tv, R.id.base_back, R.id.setting_ren_zheng_rl, R.id.setting_about_rl, R.id.setting_contact_rl, R.id.setting_version_rl, R.id.setting_update_rl})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.exit_login_tv:
                if (mExitLoginDialog == null) {
                    mExitLoginDialog = new CustomDelDialog(mContext);
                    mExitLoginDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                    mExitLoginDialog.setDialogMsg("残忍退出？");
                    mExitLoginDialog.setDelDialogEvent(new CustomDelDialog.DelDialogEvent() {
                        @Override
                        public void onDelete() {
                            mExitLoginDialog.dismiss();
                            RogerSPUtils.clear(mContext);
                            AtyContainer.getInstance().finishAllActivity();
                            new Thread(new Runnable() {
                                @Override
                                public void run() {
                                    try {
                                        Bundle bundle = new Bundle();
                                        bundle.putString("tag", "goPersonal");
                                        goActivity(mContext, HostActivity.class, bundle);
                                    } catch (Exception e) {
                                    }
                                }
                            }).start();
                        }

                        @Override
                        public void onCancel() {
                            mExitLoginDialog.dismiss();
                        }
                    });
                }
                mExitLoginDialog.show();
                break;
            case R.id.base_back:
                finish();
                break;
            case R.id.setting_ren_zheng_rl:
                if (UserStringUtils.getUserStats(mContext) != 1) {
                    goActivity(mContext, AuthenticationActivity.class);
                }
                break;
            case R.id.setting_about_rl:
                break;
            case R.id.setting_contact_rl:
                break;
            case R.id.setting_version_rl:
                break;
            case R.id.setting_update_rl:
                getAppConfig();
                break;
        }
    }

    private BaseSubscriber<AppMsgBean> appConfigSubscriber;

    private void getAppConfig() {
        appConfigSubscriber = new BaseSubscriber<AppMsgBean>(mContext) {
            @Override
            public void onSuccess(final AppMsgBean appMsgBean) {
                try {
                    if (appMsgBean.getVersion().equals(VersionUtils.getVersion(mContext))) {
                        showToast("当前已是最新版本");
                    } else {
                        if (mVersionDialog == null) {
                            mVersionDialog = new CustomDelDialog(mContext);
                            mVersionDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                            mVersionDialog.setDialogMsg("发现新版本");
                            mVersionDialog.setDialogSubMsg(appMsgBean.getVersion());
                            mVersionDialog.setDelDialogEvent(new CustomDelDialog.DelDialogEvent() {
                                @Override
                                public void onDelete() {
                                    Uri uri = Uri.parse(appMsgBean.getUrl());
                                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                                    startActivity(intent);
                                    mVersionDialog.dismiss();
                                }

                                @Override
                                public void onCancel() {
                                    mVersionDialog.dismiss();
                                }
                            });
                        }
                        mVersionDialog.show();
                    }
                } catch (Exception e) {
                }

            }
        };
        ApiManager.getInstance().getAppConfig(appConfigSubscriber);
    }
}
